﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DFYE.Web.Areas.manage.Controllers
{
    using DFYE.Web.Models;
    using JULONG.WEB.Library;
    using JULONG.WEB.Library.expViewResult;
    public class studentController : Controller
    {
        //
        // GET: /manage/student/
        DataSession<entityDBContainer, student> db = new DataSession<entityDBContainer, student>();
        public ActionResult Index()
        {

            ViewData.Model = db.query();
            return View();
        }


        //
        // GET: /manage/student/Create

        public ActionResult Create()
        {
            var sc = db.newSession<studentClass>().query(d => d.able == true).ToList();
            if (sc == null) {
                sc = new List<studentClass>();
            }
            ViewBag.studentClass = sc;
            return View();
        } 

        //
        // POST: /manage/student/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            student st = new student();
            TryUpdateModel(st);

            try
            {
                st.createDate = DateTime.Now;
                st.lastDate = st.createDate;
                // TODO: Add insert logic here
                db.insert(st);

                return RedirectToAction("Index");
            }
            catch
            {
                var sc = db.newSession<studentClass>().query(d => d.able == true).ToList();
                if (sc == null)
                {
                    sc = new List<studentClass>();
                }
                ViewBag.studentClass = sc;

                return View(st);
            }
        }
        
        //
        // GET: /manage/student/Edit/5
 
        public ActionResult Edit(int id)
        {
            student st = db.get(d=>d.ID==id);
            if (st == null) return RedirectToAction("index");

            ViewData.Model = st;
            var sc = db.newSession<studentClass>().query(d => d.able == true).ToList();
            if (sc == null)
            {
                sc = new List<studentClass>();
            }
            ViewBag.studentClass = sc;
            return View();



        }

        //
        // POST: /manage/student/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            student st = db.get(d => d.ID == id);
            TryUpdateModel(st);
            try
            {
                db.save();

                return RedirectToAction("Index", new {staute = true });

            }
            catch
            {
                var sc = db.newSession<studentClass>().query(d => d.able == true).ToList();
                if (sc == null)
                {
                    sc = new List<studentClass>();
                }
                ViewBag.studentClass = sc;
                return View(st);
            }
        }


        // POST: /manage/student/Delete/5
        [HttpGet]
        public ActionResult Delete(int id)
        {
            student st = db.get(d => d.ID == id);
            ViewData.Model = st;
            try
            {
                if (db.newSession<studentDaily>().query(d=>d.studentID==id).Any()) {
                    ModelState.AddModelError("", "日志有记录");
                }

                if (db.newSession<guestBook>().query(d => d.studentID == id).Any()) {
                    ModelState.AddModelError("", "家长留言有记录");                
                }

                return View();

            }
            catch
            {
                return View();
            }
        }
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {

                db.newSession<studentDaily>().delete(d=>d.studentID==id);


                db.newSession<guestBook>().delete(d => d.studentID == id);


                db.delete(d => d.ID == id);

                // TODO: Add delete logic here

                return RedirectToAction("Index", new { staute = true });
            }
            catch
            {
                return View();
            }
        }
        [HttpPost]
        public JsonResult getNewPsw() { 
            string str = "abcdefghijklmnopqrstuvwxyz1234567890";
            string newPsw = "";
            Random ro = new Random();
            for(int i=0;i<=6;i++){
                newPsw += str[ro.Next(0,str.Length)];
            }

            return myJson.success(newPsw);
        }

    }
}
